﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElingOA6_Admin_DSJCJ.Common;
using DevExpress.Web.Mvc;
using System.Data;
using System.Text;
using ElingOA6_Admin_DSJCJ.Areas.Admin.Models;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{
    /// <summary>
    /// 用品初始化
    /// </summary>
    /// 
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class OfficeItemYearBalController : AdminBaseController
    {
        //
        // GET: /Admin/OfficeItemYearBal/
        /// <summary>
        /// 用品初始化主页
        /// </summary>
        /// <returns>视图</returns>
        [AuthorityActionFilter("m_OfficeItemYearBal", Order = 1)]
        [LogPageActionFilter("用品初始化", Order = 2)]
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public ActionResult OfficeItemYearBalList(string year)
        {
            if (year == null) {
                year =DateTime.Now.Year.ToString();
            }

            string strHtml = StrHtml(year);
            ViewData["strHtml"] = strHtml;

            ViewData["yyyy"] = GetYYYY();

            ViewData["year"] = year;

            ViewData["visible"] = true;

            if (Int32.Parse(year) != DateTime.Now.Year)
            {
                ViewData["visible"] = false;
            }

            return View("OfficeItemYearBalList");
        }
        /// <summary>
        /// 得到下拉框年度
        /// </summary>
        /// <returns>年度集合</returns>
        public List<SelectListItem> GetYYYY()
        {
            List<SelectListItem> YYYYList = new List<SelectListItem>();
            for (int i = 2007; i < DateTime.Now.Year + 1; i++)
            {
                SelectListItem item = new SelectListItem();
                item.Value = i.ToString();
                item.Text = i + "";
                YYYYList.Add(item);
            }
            return YYYYList;
        }
        /// <summary>
        /// 保存，修改
        /// </summary>
        /// <param name="itemYearBalModel">itemYearBalModel对象</param>
        /// <param name="itemModel">itemModel对象</param>
        /// <returns>视图</returns>
        public ActionResult Save(Model.ItemYearBal itemYearBalModel, Model.Item itemModel)
        {

            
            string itemNos = Fetch.Post("hditemNo");
            string[] itemNoArray = itemNos.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);

            BLL.ItemYearBal itemYearBalBll = new BLL.ItemYearBal();
            BLL.Item itemBll = new BLL.Item();
            for (int i = 0; i < itemNoArray.Length; i++)
            {
                itemYearBalModel.FYear = decimal.Parse(Fetch.Post("year"));
                itemYearBalModel.FEntityNo = GlobalConstant.ENTITYNO;
                itemYearBalModel.FItemNo = itemNoArray[i].ToString();
                itemYearBalModel.FQty = decimal.Parse(Fetch.Post("qty" + i));
                itemYearBalModel.FPrice = decimal.Parse(Fetch.Post("price" + i));
                itemYearBalModel.FAmount = decimal.Parse(Fetch.Post("amount" + i));


                itemModel.FEntityNo = GlobalConstant.ENTITYNO;
                itemModel.FItemNo = itemNoArray[i].ToString();
                itemModel.FQty = decimal.Parse(Fetch.Post("qty" + i));
                itemModel.FPrice = decimal.Parse(Fetch.Post("price" + i));
                itemModel.FAmount = decimal.Parse(Fetch.Post("amount" + i));

                string year = Fetch.Post("year");
                string entityNo = GlobalConstant.ENTITYNO;
                string itemNo = itemNoArray[i].ToString();


                try
                {
                    /////初始化表
                    //if (itemYearBalBll.Exists(decimal.Parse(year), itemNo, entityNo))
                    //{
                    //    itemYearBalBll.Update(itemYearBalModel);
                    //}
                    //else
                    //{
                    //    //itemYearBalBll.Add(itemYearBalModel);
                    //}

                    /////管理表更新3个字段
                    //if (itemBll.Exists(itemNo, entityNo))
                    //{
                    //    itemBll.UpdateThreeCoum(itemModel);
                    //}
                    //else
                    //{
                    //    //itemBll.Add();
                    //}

                    itemYearBalBll.Update(itemYearBalModel, itemModel);

                    ViewData["jsAlert"] = "操作成功!";
                }
                catch (Exception){
                    ViewData["jsAlert"] = "操作失败!";
                }
            }
            string now = Fetch.Post("year");
            ViewData["visible"] = true;

            return OfficeItemYearBalList(now);
        }

        /// <summary>
        /// 得到一个table
        /// </summary>
        /// <param name="year">年度</param>
        /// <returns>字符串</returns>
        private string StrHtml(string year)
        {
            StringBuilder strHtml = new StringBuilder();

            BLL.Item itemBll = new BLL.Item();

            string where = "";
            if (year != null) {
                where = " FYear= " + year;
            }

            BLL.ItemYearBal itemYearBalBll = new BLL.ItemYearBal();

            DataSet ds = itemYearBalBll.GetAllList(where);
            List<Model.ItemYearBal> itemYearBalList = itemYearBalBll.DataTableToListItemYearBal(ds.Tables[0]);

            int i = 0;
            decimal qty = 0;
            decimal amount = 0;
            strHtml.Append("<table border='1' cellpadding='0' cellspacing='0' bordercolordark='#ffffff' width='100%'>");
            strHtml.Append("<tr>");
            strHtml.Append("<td align='center' height='25'>用品名称</td>");
            strHtml.Append("<td align='center'>年初数量</td>");
            strHtml.Append("<td align='center'>平均单价</td>");
            strHtml.Append("<td align='center'>库存金额</td>");
            strHtml.Append("</tr>");

            if (Int32.Parse(year) != DateTime.Now.Year) {
                foreach (Model.ItemYearBal itemYearBal in itemYearBalList)
                {
                    strHtml.Append("<tr>");
                    strHtml.Append("<td align='center' height='25'>" + itemYearBal.FItemName + "</td>");
                    string fqty = itemYearBal.FQty.ToString() == "" ? "0" : itemYearBal.FQty.ToString();
                    strHtml.Append("<td align='right'><font size='3px'>" + fqty + "</font></td>");
                    string fprice = itemYearBal.FPrice.ToString() == "" ? "0" : itemYearBal.FPrice.ToString();
                    strHtml.Append("<td align='right'><font size='3px'>" + fprice + "</font></td>");
                    string famount = itemYearBal.FAmount.ToString() == "" ? "0" : itemYearBal.FAmount.ToString();
                    strHtml.Append("<td align='right'><font size='3px'>" + famount + "</font></td>");
                    strHtml.Append("</tr>");
                    i++;
                    if (i == itemYearBalList.Count)
                    {
                        ViewData["itemNo"] += itemYearBal.FItemNo;
                    }
                    else
                    {
                        ViewData["itemNo"] += itemYearBal.FItemNo + ",";
                    }
                    qty += decimal.Parse(itemYearBal.FQty.ToString() == "" ? "0" : itemYearBal.FQty.ToString());
                    amount += decimal.Parse(itemYearBal.FAmount.ToString() == "" ? "0" : itemYearBal.FAmount.ToString());

                }
            
            }
            else
            {

                foreach (Model.ItemYearBal itemYearBal in itemYearBalList)
                {
                    strHtml.Append("<tr>");
                    strHtml.Append("<td align='center' height='25'>" + itemYearBal.FItemName + "</td>");
                    string fqty = itemYearBal.FQty.ToString() == "" ? "0" : itemYearBal.FQty.ToString();
                    strHtml.Append("<td align='right'><input class='num' id='qty" + i + "' name='qty" + i + "' type='text' value='" + fqty + "' height='25' onkeyup='qty()' /></td>");
                    string fprice = itemYearBal.FPrice.ToString() == "" ? "0" : itemYearBal.FPrice.ToString();
                    strHtml.Append("<td align='right'><input class='num' id='price" + i + "' name='price" + i + "' type='text' value='" + fprice + "' height='25' onkeyup='price()'  /></td>");
                    string famount = itemYearBal.FAmount.ToString() == "" ? "0" : itemYearBal.FAmount.ToString();
                    strHtml.Append("<td align='right'><input class='num' id='amount" + i + "' name='amount" + i + "' type='text' value='" + famount + "' height='25' onkeyup='amount()'  /></td>");
                    strHtml.Append("</tr>");
                    i++;
                    if (i == itemYearBalList.Count)
                    {
                        ViewData["itemNo"] += itemYearBal.FItemNo;
                    }
                    else
                    {
                        ViewData["itemNo"] += itemYearBal.FItemNo + ",";
                    }
                    qty += decimal.Parse(itemYearBal.FQty.ToString() == "" ? "0" : itemYearBal.FQty.ToString());
                    amount += decimal.Parse(itemYearBal.FAmount.ToString() == "" ? "0" : itemYearBal.FAmount.ToString());

                }
            }
            strHtml.Append("<tr>");
            strHtml.Append("<td align='center' height='25'>合计</td>");
            strHtml.Append("<td align='right'><font size='4px'>" + qty + "</font></td>");
            strHtml.Append("<td align='right'>/</td>");
            strHtml.Append("<td align='right'><font size='4px'>" + amount + "</font></td>");
            strHtml.Append("</tr>");
            strHtml.Append("</table>");


            return strHtml.ToString();
        }

    }
}
